<template>
  <!--  <el-dialog v-model="visible" :close-on-click-modal="false" destroy-on-close width="60%" @close="closeMatter" title="来访接待">-->
  <!-- <el-form :model="formData" :rules="formRules" size="small" :inline="false" label-width="100px">
    <el-form-item label="事项主题" prop="title">
      <el-input v-model="formData.title" placeholder="请填写事项主题" maxlength="32" clearable/>
    </el-form-item>
    <el-form-item label="事项描述">
      <editor v-model="formData.content" height="160px"/>
    </el-form-item>
    <el-form-item label="来访时间">
      <div style="max-width: 400px">
        <el-date-picker
            v-if="dateType"
            v-model="dateTime"
            style="width: 100%"
            type="datetimerange"
            range-separator="至"
            start-placeholder="来访开始时间"
            end-placeholder="来访结束时间"
            value-format="YYYY-MM-DD HH:mm:ss"
            @change="selectTime"
        />
        <el-date-picker
            v-else
            v-model="formData.actionStartTime"
            type="datetime"
            style="width: 100%"
            value-format="YYYY-MM-DD HH:mm:ss"
            placeholder="请选择来访时间"
        />
      </div>
      <el-button style="margin-left: 10px" plain :icon="Refresh" @click="dateType = !dateType"/>
    </el-form-item>
    <el-form-item label="执行人">
      <el-select v-model="formData.executorIds" placeholder="请选择执行人" size="small" multiple filterable style="width: 100%" @change="executorChange">
        <el-option v-for="item in enterpriseUsers" :key="item.userId" :value="item.userId" :label="item.nickname"/>
      </el-select>
    </el-form-item>
    <el-form-item label="关联">
      <module-relate :relate-items="formData.relateItems" @change="relateChange"/>
    </el-form-item>
    <el-form-item label="联系人">
      <el-select v-model="formData.contactUserId" placeholder="请选择联系人" clearable>
        <el-option v-for="item in contactUsers" :key="item.id" :label="item.name" :value="item.id"/>
      </el-select>
    </el-form-item>
    <el-form-item label="完成情况">
      <el-input v-model="formData.completeDiscuss" placeholder="请填写完成情况" type="textarea" :rows="3" />
    </el-form-item>
    <el-form-item label="备注信息">
      <el-input v-model="formData.remark" :rows="3" type="textarea"/>
    </el-form-item>
    <el-form-item label="上传附件">
      <Document style="margin-left: 30px;" v-if="visible" :file-group="formData.type" v-model:resultList="attach" :limit="9"/>
    </el-form-item>
    <el-form-item label="优先度">
      <el-radio-group v-model="formData.priority" size="small">
        <el-radio :label="0" size="small">普通</el-radio>
        <el-radio :label="1" size="small">高</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="通知时间" prop="remindState">
      <el-radio-group v-model="formData.remindState">
        <el-radio v-for="item in remindTimes" :key="item.value" :label="item.value" size="small">{{ item.label }}</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="通知方式" prop="remindType">
      <el-checkbox-group v-model="formData.remindType" size="small">
        <el-checkbox label="1">应用、公众号通知</el-checkbox>
        <el-checkbox label="2">短信通知</el-checkbox>
      </el-checkbox-group>
    </el-form-item>
  </el-form> -->
  <visit-reception-form ref="form" :code="code" :relateItem="relateItem" :enterpriseUsers="enterpriseUsers" :remindTimes="remindTimes"></visit-reception-form>
  <el-row justify="end">
    <el-button size="small" @click="closeMatter">关闭</el-button>
    <el-button size="small" type="primary" @click="submitData">提交</el-button>
  </el-row>
  <!--    <template #footer>-->
  <!--        <span class="dialog-footer">-->
  <!--          <el-button size="small" @click="closeMatter">关闭</el-button>-->
  <!--          <el-button size="small" type="primary" @click="submitData">提交</el-button>-->
  <!--        </span>-->
  <!--    </template>-->
  <!--  </el-dialog>-->
</template>

<script>
// 来访接待 VISIT_RECEPTION
import {computed, reactive, toRefs, ref} from 'vue'
// import Cookies from 'js-cookie'
// import {getContactList} from '/@/api/ums/customer/contact'
// import {ElMessage} from 'element-plus'
// import {Refresh} from '@element-plus/icons-vue'
// import Editor from '/@/components/editor/index.vue'
// import ModuleRelate from '/@/views/matter/components/ModuleRelate.vue'
// import Document from '/@/components/document/index.vue'
// import {formatDate} from '/@/utils/formatTime'
// import {createMatterJust} from '/@/api/matter'
// import api from '/@/api/commonApi'
import VisitReceptionForm from '/@/views/matter/components/VisitReceptionForm.vue'

export default {
  name: 'VisitReception',
  emits: ['update:code'],
  components: {
    VisitReceptionForm
  },
  props: {
    code: {
      type: String,
      default: ''
    },
    relateItem: {
      type: Object,
      default: null
    },
    enterpriseUsers: {
      type: Array,
      default: []
    },
    remindTimes: {
      type: Array,
      default: []
    }
  },
  setup(props, context) {
    // const {code, relateItem, enterpriseUsers, remindTimes} = toRefs(props)
    const form = ref()
    // const visible = computed({
    //   get: () => {
    //     let flag = code.value == 'VISIT_RECEPTION'
    //     // if (flag) {
    //     //   // 默认带入自己
    //     //   state.selfId = Cookies.get('userId')
    //     //   state.formData.executorIds = [state.selfId]
    //     //   // 行动时间默认现在
    //     //   let nowTime = formatDate(new Date(), 'YYYY-mm-dd HH:MM:SS')
    //     //   state.formData.actionStartTime = nowTime
    //     //   state.dateTime = [nowTime, nowTime]
    //     //   if (relateItem.value) {   // 关联项带入
    //     //     state.formData.relateItems = [relateItem.value]
    //     //   }
    //     // }
    //     return flag
    //   },
    //   set: val => context.emit('update:code', val ? 'VISIT_RECEPTION' : '')
    // })
    // const state = reactive({
    //   formData: {
    //     title: '来访接待',
    //     content: '',
    //     actionStartTime: null,
    //     actionEndTime: null,
    //     executorIds: [],
    //     relateItems: [],
    //     contactUserId: null,
    //     completeDiscuss: null,
    //     attach: '',
    //     attachNames: '',
    //     type: 'VISIT_RECEPTION',    // 事项类型编码
    //     remark: null,
    //     priority: 0,
    //     remindState: 'NONE',
    //     remindType: [],
    //     needConfirm: 1  // 是否需要确认完成, 0不需要 1需要
    //   },
    //   formRules: {
    //     title: {required: true, message: '请填写事项主题', trigger: 'blur'}
    //   },
    //   selfId: null,
    //   dateType: true,
    //   dateTime: [],
    //   contactUsers: [],
    //   attach: [],   // 附件/文档列表
    // })

    /**
     * 关联项改动，判断是否有客户被选择，获取联系人
     */
    // const relateChange = (items) => {
    //   if (items.length > 0) {
    //     let customerIds = []
    //     items.forEach(relate => {
    //       // 依据id和名称判断是不是客
    //       if (relate.agendaMatterRelatableId === '1640265288376561666' || relate.label.indexOf('客') > -1) {
    //         customerIds.push(relate.seRelateDataId)
    //       }
    //     })
    //     if (customerIds.length > 0) {
    //       try {
    //         getContactList({customerIds: customerIds + ''}).then(res => {
    //           state.contactUsers = res.data
    //           // 置空已选联系人
    //           state.formData.contactUserId = null
    //         })
    //       } catch (e) {
    //         console.log(e)
    //       }
    //     }
    //   }
    // }
    // const selectTime = (val) => {
    //   if (val && val.length === 2) {
    //     state.formData.actionStartTime = val[0]
    //     state.formData.actionEndTime = val[1]
    //   } else {
    //     state.formData.actionStartTime = state.formData.actionEndTime = null
    //   }
    // }
    // const executorChange = (val) => {
    //   if (!val.includes(state.selfId)) {
    //     state.formData.executorIds.push(state.selfId)
    //     ElMessage.warning('本人必须在执行人中，无法移除')
    //   }
    // }
    /**
     * 提交事项
     */
    const submitData = () => {
      form.value.submit()
      // let params = Object.assign({}, state.formData)
      // // 列表转逗号分割字符串提交
      // params.remindType = params.remindType + ''
      // if (state.attach.length > 0) {  // 组装附件地址和名称
      //   for (let i = 0; i < state.attach.length; i++) {
      //     let item = state.attach[i]
      //     params.attach += item.src
      //     params.attachNames += item.name
      //     if (i < state.attach.length - 1) {
      //       params.attach += ','
      //       params.attachNames += '，'
      //     }
      //   }
      // }
      // createMatterJust(params).then(res => {
      //   if (res.data) {
      //     ElMessage.success('提交成功！')
      //     // 关联事项主键
      //     let data = state.attach.map(item => {
      //       item.matterId = res.data.agendaMatterId
      //       item.matterName = res.data.title
      //     })
      //     api.fileRelevanceModule(data)
      //     closeMatter()
      //   }
      // }).catch(e => console.log(e))
    }
    const closeMatter = () => {
      context.emit('update:code', '')
      form.value.reset()
    }

    return {
      // ...toRefs(state),
      form,
      // relateChange,
      submitData,
      // selectTime,
      // executorChange,
      // Refresh,
      closeMatter,
      // remindTimes,
      visible
      // enterpriseUsers
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
